/*
 * Update.cpp
 *
 *  Created on: May 13, 2012
 *      Author: kobe
 */

#include "Update.h"

Update::Update(std::string input) :
		Proposition(UPDATE) {
	std::string content(input, 2, input.size() - 2);
	size_t left, right;
	std::string sleft = getBracketContent(content, &left, &right);
	std::string ncontent(content, right + 2, content.size() - right - 2);
	std::string sright = getBracketContent(ncontent, &left, &right);
	mProposition = createProposition(sleft);
	mAction = createProposition(sright);
	if (!mAction->isAction()) {
		std::cout << "Parse error, update right is not action" << std::endl;
		std::cout << mAction->toString();
	} else if (mProposition->isAction()) {
		std::cout << "Parse error, update left is not proposition" << std::endl;
		std::cout << mProposition->toString() << std::endl;
	}
}

Update::Update(Proposition* left, Proposition* right) :
		Proposition(UPDATE) {
	mProposition = left;
	mAction = right;
	if (!mAction->isAction()) {
		std::cout << "Parse error, update right is not action" << std::endl;
	} else if (mProposition->isAction()) {
		std::cout << "Parse error, update left is not proposition" << std::endl;
	}
}

Update::~Update() {
// TODO Auto-generated destructor stub
}

bool Update::isAction() {
	return false;
}

Proposition *Update::getProposition() const {
	return mProposition;
}

Proposition *Update::getAction() const {
	return mAction;
}

bool Update::compare(Proposition* p) {
	if (p->getPropName() != UPDATE) {
		return false;
	} else {
		Update *up = (Update*) p;
		return mProposition->compare(up->getProposition())
				&& mAction->compare(up->getAction());
	}
}

std::string Update::toString() {
	std::string result;
	result += "up{";
	result += mProposition->toString();
	result += "}{";
	result += mAction->toString();
	result += '}';
	return result;
}

std::string Update::toLatex() {
	std::string s;
	if (mProposition->getPropName() != ATOMIC) {
		s += "(";
	}
	s += mProposition->toLatex();
	if (mProposition->getPropName() != ATOMIC) {
		s += ")";
	}

	s += " \\cdot ";

	if (mAction->getPropName() != ATOMIC && mAction->getPropName() != ACTION) {
		s += "(";
	}
	s += mAction->toLatex();
	if (mAction->getPropName() != ATOMIC && mAction->getPropName() != ACTION) {
		s += ")";
	}

	return s;
}

